<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/10/17
 * Time: 10:48
 */

namespace app\api\validate;


use app\lib\exception\WorkDepartmentException;

class WorkMemberParameter extends BaseValidate
{

    protected $rule=[
        'name'=>'require',
        'd_id'=>'require|checkDepartments',
        'isleader'=>'checkLeader',
        'bind_phone'=>'require|isMobile',
        'phone'=>'require|isMobile',
        'position'=>'require',
        'userId'=>'require',
        'gender'=>'require',
        'email'=>'require',
        'id'=>'require'
    ];

    protected $message=[
        'name'=>'成员名称不能为空',
        'bind_phone'=>'微信绑定电话不能为空，或者电话格式错误',
        'phone'=>'展示电话不能为空，或者电话格式错误',
        'position'=>'职位不能为空',
        'gender'=>'性别不能为空',
        'email'=>'邮箱不能为空',
        'id'=>'自增id不能为空'
    ];


    /**
     * 验证部门
     * @param $values
     * @return bool
     * @throws WorkDepartmentException
     */
    protected function checkDepartments($values){
        if (empty($values)){
            throw new WorkDepartmentException([
                'code'=>400,
                'msg'=>'部门ID不能为空',
                'error_code'=>43001
            ]);
        }
        if (!is_array($values)){
            throw new WorkDepartmentException([
                'code'=>400,
                'msg'=>'参数错误',
                'error_code'=>43001
            ]);
        }
        foreach ($values as $k=>$v){
            $this->checkDepartment($v);
        }
        return true;
    }
    /**
     * 单个部门验证规则,手动调用
     * @param $department
     * @return bool
     * @throws WorkDepartmentException
     */
    protected function checkDepartment($department){
        if (is_numeric($department)&& is_int($department+0)&& ($department+0)>0){
            return true;
        }else{
            throw new WorkDepartmentException([
                'code'=>400,
                'msg'=>'部门id必须为正整数',
                'error_code'=>43001
            ]);
        }
    }

    /**
     * 部门上级检查
     * @return bool
     */
    protected function checkLeader(){
        return true;
    }


}